package com.example.tass.controller;

import cn.tass.SRJ1933.devices.inf.TassSignServerApi;
import cn.tass.SRJ1933.devices.params.request.digest.DigestFinalRequest;
import cn.tass.SRJ1933.devices.params.request.digest.DigestInitRequest;
import cn.tass.SRJ1933.devices.params.request.digest.DigestRequest;
import cn.tass.SRJ1933.devices.params.request.digest.DigestUpdateRequest;
import cn.tass.SRJ1933.devices.params.response.digest.DigestFinalResponse;
import cn.tass.SRJ1933.devices.params.response.digest.DigestInitResponse;
import cn.tass.SRJ1933.devices.params.response.digest.DigestResponse;
import cn.tass.SRJ1933.devices.params.response.digest.DigestUpdateResponse;
import cn.tass.exceptions.TAException;
import com.example.tass.common.ResultMessage;
import com.example.tass.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

// 3.7 摘要计算
@RestController
public class DigestTest_3_7 {

    private static String printFormat = "错误码[%d]\n错误消息[%s]\n接口结果： %s\n";
    private static final Logger logger = LoggerFactory.getLogger(DigestTest_3_7.class);
    private TassSignServerApi signServerApi;

    public DigestTest_3_7(TassSignServerApi signServerApi) {
        this.signServerApi = signServerApi;
    }

    // 3.7.1.1 数据摘要（非国密带ID） 摘要算法： SHA1、SHA224、SHA256、SHA384、SHA512、SM3
    @GetMapping(value = "digest")
    public ResultMessage digest(@RequestBody DigestRequest request) {
        String data = request.getData();
        try {
            if (!StringUtils.isBase64(request.getData())) {
                data = StringUtils.stringToBase64String(data);
            }
            DigestResponse response = signServerApi.digest(request.getAlgorithm(), data);
            if (response.getCode() == 0) {
                return ResultMessage.newSuccessResult("摘要结果：" + response.getDigest());
            } else {
                return ResultMessage.newFailureResult("获取摘要失败");
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.7.1.2 数据摘要（国密带ID，内部密钥）
    @GetMapping(value = "/digestWithGMID")
    public ResultMessage digestWithGMID(@RequestBody DigestRequest request) {
        try {
            DigestResponse response = signServerApi.digest(request.getAlgorithm(), request.getSm2KeyId(),
                    request.getSm2KeyIdType(), request.getSm2UserID(), request.getData());
            if (response.getCode() == 0) {
                return ResultMessage.newSuccessResult(response.getDigest());
            } else {
                return ResultMessage.newFailureResult("获取摘要失败");
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    //3.7.1.3 数据摘要（国密带ID，外部公钥或证书）
    @GetMapping(value = "/digestExtKey")
    public ResultMessage digestExtKey(@RequestBody DigestRequest request) {
        try {
            DigestResponse response = signServerApi.digestExtKey(request.getAlgorithm(), request.getSm2PubKeyInfo(),
                    request.getSm2PubKeyInfoType(), request.getSm2UserID(), request.getData());
            if (response.getCode() == 0) {
                return ResultMessage.newSuccessResult(response.getDigest());
            } else {
                return ResultMessage.newFailureResult("获取摘要失败");
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }
    ///////// 3.7.2数据多包摘要（初始化）///////////////

    // 3.7.2.1 数据多包摘要（初始化 – 非国密，内部密钥）
    @GetMapping(value = "/digestInit")
    public ResultMessage digestInit(@RequestBody DigestInitRequest request) {
        try {
            DigestInitResponse response = signServerApi.digestInit(request.getAlgorithm());
            if (response.getCode() == 0) {
                return ResultMessage.newSuccessResult(response.getDigestHandle());
            } else {
                return ResultMessage.newFailureResult("获取摘要会话句柄失败");
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.7.2.2 数据多包摘要（初始化 – 国密，内部密钥）
    // TODO

    // 3.7.2.3 数据多包摘要（初始化 – 国密，外部公钥或证书）
    // TODO

    // 3.7.3 数据多包摘要（更新数据）
    @GetMapping(value = "/digestUpdate")
    public ResultMessage digestUpdate(@RequestBody DigestUpdateRequest request) {
        try {
            DigestUpdateResponse response = signServerApi.digestUpdate(request.getDigestHandle(), request.getData());
            if (response.getCode() == 0) {
                return ResultMessage.newSuccessResult(response.getDigestHandle());
            } else {
                return ResultMessage.newFailureResult("获取摘要会话句柄失败");
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.7.4 数据多包摘要（结束摘要）
    @GetMapping(value = "/digestFinal")
    public ResultMessage digestFinal(@RequestBody DigestFinalRequest request) {
        try {
            DigestFinalResponse response = signServerApi.digestFinal(request.getDigestHandle());
            if (response.getCode() != 0) {
                return ResultMessage.newFailureResult("获取摘要失败");
            } else {
                return ResultMessage.newSuccessResult(response.getDigest());
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }
    
    // 3.7.5 国密签名Z值计算
    // TODO

}
